package ru.papergames.model.proxies
{
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import ru.papergames.model.proxies.HTTPProxy;
	import ru.papergames.controller.notifications.FrontendNotifications;
	import ru.papergames.model.vo.SessionDataVO;
	import ru.papergames.utils.constants.ConfigKeys;

	public class SessionDataProxy extends MainHTTPProxy
	{
		
		public static const NAME:String = 'SessionDataProxy';
		
		//methods
		public static const GET_SESSION_DATA:String = 'getSessionData';
		public static const STORE_SESSION_DATA:String = 'storeSessionData';		
		public static const PING:String = 'ping';
		
		
		//responses
		public static const GET_SESSION_DATA_START:String = GET_SESSION_DATA + HTTPProxy.START_NOTE_POSTFIX;
		public static const GET_SESSION_DATA_SUCCESS:String = GET_SESSION_DATA + HTTPProxy.SUCCESS_NOTE_POSTFIX;
		public static const GET_SESSION_DATA_FAULT:String = GET_SESSION_DATA + HTTPProxy.FAULT_NOTE_POSTFIX;
		
		public static const STORE_SESSION_DATA_START:String = STORE_SESSION_DATA + HTTPProxy.START_NOTE_POSTFIX;
		public static const STORE_SESSION_DATA_SUCCESS:String = STORE_SESSION_DATA + HTTPProxy.SUCCESS_NOTE_POSTFIX;
		public static const STORE_SESSION_DATA_FAULT:String = STORE_SESSION_DATA + HTTPProxy.FAULT_NOTE_POSTFIX;
		
		public static const PING_START:String = PING + HTTPProxy.START_NOTE_POSTFIX;
		public static const PING_SUCCESS:String = PING + HTTPProxy.SUCCESS_NOTE_POSTFIX;
		public static const PING_FAULT:String = PING + HTTPProxy.FAULT_NOTE_POSTFIX;
		
		private var _pingTimer:Timer;
		
		public function SessionDataProxy(url:String)
		{
			super(NAME, url, new SessionDataVO());
		}
		
		override public function onRegister():void {
			super.onRegister();
						
			_pingTimer = new Timer(settingsProxy.getNumberProperty(ConfigKeys.PING_INTERLEAVE, 60000), 0);
			_pingTimer.addEventListener(TimerEvent.TIMER, pingServer);
			
		}
		
		override public function onRemove():void {
			super.onRemove();
			
			_pingTimer.removeEventListener(TimerEvent.TIMER, pingServer);
			_pingTimer.stop();
			_pingTimer = null;
		}
		
		protected function get loginProxy():LoginProxy {
			return getProxy(LoginProxy.NAME) as LoginProxy;
		}
		
		public function get sessionData():SessionDataVO {
			return this.data as SessionDataVO;
		}
		
		public function startSessManPing():void {
			_pingTimer.start();
		}
		
		public function stopSessManPing():void {
			_pingTimer.stop();
		}
		
		private function pingServer(evt:TimerEvent):void {
			call(SessionDataProxy.PING);
		}
		
		override protected function parseResponse(data:XML, ownResponse:Boolean):void {
			
		}		
		
		/*****  Call handlers  *****/
		
		/* getSessionData */
		
		public function getSessionData_Result(data:XML):String {
			sessionData.isActive = String(data.data.success) == 'true';
			
			loginProxy.loginVO.isLoggedIn = sessionData.isActive;
			
			if ( !_pingTimer.running && sessionData.isActive ) {
				//facade.sendNotification(FrontendNotifications.LOGIN_SUCCESSED);
			}
						
			return sessionData.isActive ? GET_SESSION_DATA_SUCCESS : GET_SESSION_DATA_FAULT;
		}
		
		/* storeSessionData */
		
		public function ping_Result(data:XML):String {
			var isSuccess:Boolean = String(data.data.success) == 'true';
			
			//sync time + logout if success is false
			
			if (!isSuccess) {
				facade.sendNotification(FrontendNotifications.LOGOUT);
			}
			
			sessionData.timestamp = Number(data.data.timestamp);
			
			return STORE_SESSION_DATA_SUCCESS;
		}
		
	}
}